package com.hitqz.robot.biz.flow.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.google.common.cache.Cache;
import com.hitqz.robot.biz.flow.config.FlowInterruptException;
import com.yomahub.liteflow.flow.LiteflowResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.concurrent.Future;
@Slf4j
public class LiteFlowUtil {

    public static Integer ticketsId;

    public static Future<LiteflowResponse> future;

    public static void checkInterruptFlag() throws Exception {
        Cache<String,Object> flowCache = SpringUtil.getBean("flowCache");
        boolean stop =Optional.ofNullable((Boolean) flowCache.getIfPresent("stop")).orElse(false);
        if (stop){
            throw new FlowInterruptException();
        }
    }

    public static void setInterruptFlag() {
        //通过标志位去中断循环
        Cache<String,Object> flowCache = SpringUtil.getBean("flowCache");
        flowCache.put("stop",true);
        //中断组件
        if (future!=null && (!future.isCancelled() || !future.isDone())){
            log.info("尝试取消组件");
            if (future.cancel(true)){
                log.info("取消组件成功");
                future = null;
            }else {
                log.info("取消组件失败");
            }
        }
    }


}
